package com.easymeeting.controller;

import java.util.List;

import com.easymeeting.annotation.GlobalInterceptor;
import com.easymeeting.entity.dto.TokenUserInfoDto;
import com.easymeeting.entity.enums.UserContactApplyStatusEnum;
import com.easymeeting.entity.enums.UserContactStatusEnum;
import com.easymeeting.entity.po.UserContactApply;
import com.easymeeting.entity.query.UserContactApplyQuery;
import com.easymeeting.entity.query.UserContactQuery;
import com.easymeeting.entity.po.UserContact;
import com.easymeeting.entity.vo.ResponseVO;
import com.easymeeting.entity.vo.UserInfoVO4Search;
import com.easymeeting.service.UserContactApplyService;
import com.easymeeting.service.UserContactService;
import jdk.nashorn.internal.parser.Token;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * 联系人 Controller
 */
@RestController("userContactController")
@RequestMapping("/userContact")
public class UserContactController extends ABaseController {

    @Resource
    private UserContactService userContactService;
    @Resource
    private UserContactApplyService userContactApplyService;

    /**
     * 根据条件分页查询
     */
    @RequestMapping("/searchContact")
    public ResponseVO loadDataList(@NotEmpty String userId) {
        TokenUserInfoDto tokenUserInfoDto = gettokenUserInfoDto();
        UserInfoVO4Search result = userContactService.searchContact(tokenUserInfoDto.getUserId(), userId);
        return getSuccessResponseVO(result);
    }

    @RequestMapping("/contactApply")
    @GlobalInterceptor
    public ResponseVO contactApply(@NotEmpty String receiveUserId) {
        TokenUserInfoDto tokenUserInfoDto = gettokenUserInfoDto();

        UserContactApply userContactApply = new UserContactApply();
        userContactApply.setApplyUserId(tokenUserInfoDto.getUserId());
        userContactApply.setReceiveUserId(receiveUserId);
        Integer status = userContactApplyService.saveUserContactApply(userContactApply);
        return getSuccessResponseVO(status);
    }

    @RequestMapping("/dealWithApply")
    @GlobalInterceptor
    public ResponseVO dealWithApply(@NotEmpty String applyUserId, @NotNull Integer status) {
        TokenUserInfoDto tokenUserInfoDto = gettokenUserInfoDto();
        userContactApplyService.dealWithApply(applyUserId, tokenUserInfoDto.getUserId(), tokenUserInfoDto.getNickName(), status);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/loadContactUser")
    @GlobalInterceptor
    public ResponseVO loadContactUser() {
        TokenUserInfoDto tokenUserInfoDto = gettokenUserInfoDto();
        UserContactQuery userContactQuery = new UserContactQuery();
        userContactQuery.setUserId(tokenUserInfoDto.getUserId());
        userContactQuery.setStatus(UserContactStatusEnum.FRIEND.getStatus());
        userContactQuery.setQueryUserInfo(true);
        userContactQuery.setOrderBy("last_update_time desc");
        List<UserContact> userContactList = userContactService.findListByParam(userContactQuery);
        return getSuccessResponseVO(userContactList);
    }

    @RequestMapping("/loadContactApply")
    @GlobalInterceptor
    public ResponseVO loadContactApply() {
        TokenUserInfoDto tokenUserInfoDto = gettokenUserInfoDto();
        UserContactApplyQuery applyQuery = new UserContactApplyQuery();
        applyQuery.setReceiveUserId(tokenUserInfoDto.getUserId());
        applyQuery.setQueryUserInfo(true);
        applyQuery.setOrderBy("last_apply_time desc");
        List<UserContactApply> applyList = this.userContactApplyService.findListByParam(applyQuery);
        return getSuccessResponseVO(applyList);
    }

    @RequestMapping("/loadContactApplyDealWithCount")
    @GlobalInterceptor
    public ResponseVO loadContactApplyDealWithCount() {
        TokenUserInfoDto tokenUserInfoDto = gettokenUserInfoDto();
        UserContactApplyQuery applyQuery = new UserContactApplyQuery();
        applyQuery.setReceiveUserId(tokenUserInfoDto.getUserId());
        applyQuery.setStatus(UserContactApplyStatusEnum.INIT.getStatus());
        Integer count = this.userContactApplyService.findCountByParam(applyQuery);
        return getSuccessResponseVO(count);
    }

    @RequestMapping("/delContact")
    @GlobalInterceptor
    public ResponseVO delContact(@NotEmpty String contactId, @NotNull Integer status) {
        TokenUserInfoDto tokenUserInfoDto = gettokenUserInfoDto();
        userContactService.delContact(tokenUserInfoDto.getUserId(), contactId, status);
        return getSuccessResponseVO(null);
    }

}